package com.chang.multithread.CorrectPostureStep;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;


@Slf4j
public class Sleep {
    static final Object room =new Object();
    static boolean hasAfflatus=false;//有没有灵感

    public static void main(String[] args) {
        new Thread(()->{
            synchronized (room){
                log.debug("有没有灵感？[{}]",hasAfflatus);
                if (!hasAfflatus){
                    log.debug("没有灵感，休息一下");
                    try {
                        TimeUnit.SECONDS.sleep(2);
                      } catch (InterruptedException e) {
                      e.printStackTrace(); }
                }
                log.debug("有灵感么[{}]",hasAfflatus);
                if (hasAfflatus){
                    log.debug("灵感来了,准备开工！");
                }
            }
        },"t1").start();

        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                synchronized (room){
                    log.debug("可以开始干活了！！！");
                }
            },"Others").start();
        }

       try {
           TimeUnit.SECONDS.sleep(1);
         } catch (InterruptedException e) {
         e.printStackTrace(); }

       new Thread(()->{
           hasAfflatus=true;
           log.debug("送灵感的来了！！！");
       },"送灵感的").start();
    }
}
